﻿using BootstrapBlazor.OnlyServerZhongShun.Components;
using BootstrapBlazor.OnlyServerZhongShun.Data;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.SignalR;
using SqlSugar;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
builder.Services.AddResponseCompression();

builder.Services.AddRazorComponents().AddInteractiveServerComponents();

builder.Services.AddBootstrapBlazor();
// 增加 Table Excel 导出服务
builder.Services.AddBootstrapBlazorTableExportService();
builder.Services.AddControllers();

builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddScoped(it => new SqlSugarScope(new List<ConnectionConfig> {
        new ConnectionConfig()
        {
            //正式
           ConnectionString =  builder.Configuration.GetConnectionString("sqlserver"),//正式连接符字串
            DbType = DbType.SqlServer,//数据库类型
            IsAutoCloseConnection = true, //不设成true要手动close       
        }
        },
           db =>
           {
               ////也可以这里面循环
               //db.GetConnection("ncc").Aop.OnLogExecuting = (sql, pars) =>
               //{
               //    log.Warn("1执行ncc库ConnectionString:" + db.GetConnection("ncc").CurrentConnectionConfig.ConnectionString);
               //    log.Warn("2执行ncc库sql:" + sql);
               //    log.Warn(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));//参数
               //};
               //db.GetConnection("sqlserver").Aop.OnLogExecuting = (sql, pars) =>
               //{
               //    log.Warn("1执行sqlserver库ConnectionString:" + db.GetConnection("sqlserver").CurrentConnectionConfig.ConnectionString);
               //    log.Warn("执行sqlserver库sql:" + sql);
               //    log.Warn(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));//参数
               //};

           }
           ));

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
builder.Services.AddMvc();
builder.Services.AddHttpContextAccessor();

// 增加 Table 数据服务操作类
builder.Services.AddTableDemoDataService();

// 增加 SignalR 服务数据传输大小限制配置
builder.Services.Configure<HubOptions>(option => option.MaximumReceiveMessageSize = null);




var app = builder.Build();

//if (!app.Environment.IsDevelopment())
//{
    app.UseExceptionHandler("/Error");
    app.UseResponseCompression();
//}

app.UseStaticFiles();
app.UseResponseCompression();

app.UseRouting();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.UseAntiforgery();

app.MapRazorComponents<App>().AddInteractiveServerRenderMode();

app.Run();
